﻿using System.Collections.Concurrent;
using System.Linq;

namespace Fetches.Fetch.PagePool
{
    public abstract class LocalPagePool : AbstractPagePool
    {
        protected readonly ConcurrentDictionary<int, string> PagesIndex = new ConcurrentDictionary<int, string>();

        public override int ValidateOffset(int offset)
        {
            if (offset < 0)
            {
                return 0;
            }
            if (PagesIndex.ContainsKey(offset))
            {
                return offset;
            }

            if (PagesIndex.Count == 0 || offset > PagesIndex.Keys.Max())
            {
                return -1;
            }

            return PagesIndex.Keys.OrderBy(k => k).Where(k => k > offset).Take(1).First();
        }

    }
}
